package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    //分页查询角色信息
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();

        PageHelper.startPage(currentPage, pageSize);

        Page<Role> page = roleDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total, rows);
    }

<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    //添加角色
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleDao.add(role);
        Integer id = role.getId();
        this.setRoleAndPermission(id, permissionIds);
        this.setRoleAndMenu(id, menuIds);
<<<<<<< HEAD

    }

   //回写角色信息
    public Role findById(Integer id) {
       Role role =  roleDao.findById(id);
        return role;
    }

=======
    }

    @Override
    //回写角色信息
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    //编辑角色
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {

        roleDao.edit(role);
        roleDao.deleteAssociationWithPermission(role.getId());
        roleDao.deleteAssociationWithMenu(role.getId());
<<<<<<< HEAD
        this.setRoleAndPermission(role.getId(),permissionIds);
        this.setRoleAndMenu(role.getId(),menuIds);
    }

  //根据id删除角色
    public Boolean deleteById(Integer role_id) {
       Long count1 = roleDao.findPermissionCountById(role_id);
       if(count1 > 0){
           return false;
       }
        Long count2 = roleDao.findMenuCountById(role_id);
       if(count2>0){
           return false;
       }
       roleDao.deleteById(role_id);
=======
        this.setRoleAndPermission(role.getId(), permissionIds);
        this.setRoleAndMenu(role.getId(), menuIds);
    }
@Override
    //根据id删除角色
    public Boolean deleteById(Integer role_id) {
        Long count1 = roleDao.findPermissionCountById(role_id);
        if (count1 > 0) {
            return false;
        }
        Long count2 = roleDao.findMenuCountById(role_id);
        if (count2 > 0) {
            return false;
        }
        roleDao.deleteById(role_id);
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
        return true;
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return roleDao.findRoleIdsByUserId(userId);
    }

    //设置角色和权限关系
    public void setRoleAndPermission(Integer role_id, Integer[] permisssionIds) {
        if (permisssionIds != null && permisssionIds.length > 0) {
            for (Integer permisssion_id : permisssionIds) {
                Map<String, Integer> map = new HashMap<>();
<<<<<<< HEAD
                map.put("role_id",role_id);
                map.put("permission_id",permisssion_id);
=======
                map.put("role_id", role_id);
                map.put("permission_id", permisssion_id);
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //设置角色和菜单关系
    public void setRoleAndMenu(Integer role_id, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menu_id : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", role_id);
                map.put("menu_id", menu_id);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
